package gov.pr.celepar.sentinela.admin.action;

import gov.pr.celepar.sentinela.Constants;
import gov.pr.celepar.sentinela.admin.dao.UsuarioDAO;
import gov.pr.celepar.sentinela.admin.dao.factory.DAOFactoryServer;
import gov.pr.celepar.sentinela.admin.form.RootForm;
import gov.pr.celepar.sentinela.admin.pojo.Usuario;
import gov.pr.celepar.sentinela.comunicacao.SentinelaComunicacao;
import gov.pr.celepar.sentinela.comunicacao.SentinelaInterface;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

public class RootAction extends BaseAction {
	private UsuarioDAO usuarioB = null;

	public String executar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RootForm rootForm = (RootForm) form;
		DAOFactoryServer daoFactory = DAOFactoryServer.getDAOFactory();
		this.usuarioB = daoFactory.getUsuarioDAO();
		SentinelaInterface si = SentinelaComunicacao.getInstance(request);

		if (this.CONF_INCLUIR.equalsIgnoreCase(rootForm.getAcao())) {
			this.comunicacao.autorizaOperacao('I', request, response);
			incluir(rootForm, si, request);
			String msg = this.getResources(request).getMessage(
					"sucesso.inc_root");
			this.addMessageSucess(msg, request);
			return "lista_root";
		} else if (this.CONF_EXCLUIR.equalsIgnoreCase(rootForm.getAcao())) {
			this.comunicacao.autorizaOperacao('E', request, response);
			excluir(rootForm, si, request);
			String msg = this.getResources(request).getMessage(
					"sucesso.exc_root");
			this.addMessageSucess(msg, request);
			return "lista_root";
		} else if (this.INCLUIR.equalsIgnoreCase(rootForm.getAcao())) {
			this.comunicacao.autorizaOperacao('I', request, response);
			return "incluir_root";
		} else if (this.EXCLUIR.equalsIgnoreCase(rootForm.getAcao())) {
			this.comunicacao.autorizaOperacao('E', request, response);
			consultar(rootForm);
			return "excluir_root";
		}
		return "lista_root";
	}

	private String incluir(RootForm form, SentinelaInterface si,
			HttpServletRequest request) throws Exception {
		Usuario user = new Usuario();
		user.setCodUsuario(Long.decode(form.getCodUsuario()));
		user.setRoot(true);

		this.usuarioB.atualizarUsuarioRoot(user, si.getCodUsuario(), request
				.getRemoteAddr(), Constants.SENTINELA_INTERFACE_ADMIN, si
				.getCodSistema());

		return "sucesso_inc_usuario";
	}

	private String excluir(RootForm form, SentinelaInterface si,
			HttpServletRequest request) throws Exception {
		Usuario user = new Usuario();
		user.setCodUsuario(Long.decode(form.getCodUsuario()));
		user.setRoot(false);

		this.usuarioB.atualizarUsuarioRoot(user, si.getCodUsuario(), request
				.getRemoteAddr(), Constants.SENTINELA_INTERFACE_ADMIN, si
				.getCodSistema());

		return "sucesso_inc_usuario";
	}

	private String consultar(RootForm form) throws Exception {
		Usuario user = this.usuarioB.consultar(Long
				.decode(form.getCodUsuario()));
		form.setNome(user.getNome());
		return "sucesso_inc_usuario";
	}
}
